
package com.shop.cloud.mall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.mall.common.entity.PageDevise;
import com.shop.cloud.mall.admin.mapper.PageDeviseMapper;
import com.shop.cloud.mall.admin.service.PageDeviseService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;

/**
 * 页面设计表
 *
 * @author
 * @date 2020-09-22 10:44:06
 */
@Service
@AllArgsConstructor
public class PageDeviseServiceImpl extends ServiceImpl<PageDeviseMapper, PageDevise> implements PageDeviseService {

	private final RedisTemplate redisTemplate;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(PageDevise entity) {
		if(CommonConstants.YES.equals(entity.getEnable())){
			//如果开启则关闭其他装修
			PageDevise pageDevise1 = new PageDevise();
			pageDevise1.setEnable(CommonConstants.NO);
			baseMapper.update(pageDevise1, Wrappers.<PageDevise>lambdaQuery()
					.eq(PageDevise::getEnable, CommonConstants.YES)
					.eq(PageDevise::getPageType, entity.getPageType())
					.eq(PageDevise::getClientType, entity.getClientType())
					.eq(PageDevise::getShopId, entity.getShopId())
					.eq(PageDevise::getAppId, entity.getAppId()));
		}
		//清理缓存
		clearRedis(entity);
		return super.save(entity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateById(PageDevise entity) {
		PageDevise pageDevise = baseMapper.selectById(entity.getId());
		if(CommonConstants.YES.equals(entity.getEnable())){
			//如果开启则关闭其他装修
			PageDevise pageDevise1 = new PageDevise();
			pageDevise1.setEnable(CommonConstants.NO);
			baseMapper.update(pageDevise1, Wrappers.<PageDevise>lambdaQuery()
					.eq(PageDevise::getEnable, CommonConstants.YES)
					.eq(PageDevise::getPageType, pageDevise.getPageType())
					.eq(PageDevise::getClientType, pageDevise.getClientType())
					.eq(PageDevise::getShopId, pageDevise.getShopId())
					.eq(PageDevise::getAppId, pageDevise.getAppId()));
		}
		baseMapper.updateById(entity);
		//清理缓存
		clearRedis(pageDevise);
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean removeById(Serializable id) {
		PageDevise pageDevise = baseMapper.selectById(id);
		baseMapper.deleteById(id);
		//清理缓存
		clearRedis(pageDevise);
		return Boolean.TRUE;
	}

	/**
	 * //清理缓存
	 * @param pageDevise
	 */
	void clearRedis(PageDevise pageDevise){
		//清理缓存
		String key = StrUtil.format("{}:{}:{}:{}:{}:{}",
				pageDevise.getTenantId(),
				CacheConstants.MALL_PAGE_DEVISE_CACHE,
				pageDevise.getPageType(),
				pageDevise.getClientType(),
				pageDevise.getShopId(),
				pageDevise.getAppId());
		redisTemplate.delete(key);
	}
}
